//
//  TSMuneView.m
//  Cabbage
//
//  Created by TooCMS on 16/9/20.
//  Copyright © 2016年 TooCMS. All rights reserved.
//
#define TAG 1000
#import "TSMuneView.h"
#import "TSMenuBtnView.h"
//外界传进来的view的高度
static const CGFloat MuneHeight =200+5;
/**一排4个||一排5个*/
static  CGFloat Item ;


@interface TSMuneView ()<UIScrollViewDelegate>
{
    UIView *_backView1;
    UIView *_backView2;
//    UIPageControl *_pageControl;
}
@end
@implementation TSMuneView

-(instancetype)initWithmenuArray:(NSArray *)titleArray andImageArray:(NSArray *)imageArray andFrame:(CGRect)frameCustom Layout:(NSInteger)Layout{
    if(self = [super init]) {
        Item=Layout;
        _backView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frameCustom.size.width, frameCustom.size.height)];
        _backView1.backgroundColor = [UIColor whiteColor];
        _backView2 = [[UIView alloc]initWithFrame:CGRectMake(frameCustom.size.width, 0, frameCustom.size.width, frameCustom.size.height)];
        _backView2.backgroundColor = [UIColor whiteColor];
        UIScrollView *scrollView = [[UIScrollView alloc]init];
        scrollView.bounces = NO;
        if (titleArray.count > Item) {
            if (titleArray.count > Item*2) {
                scrollView.frame = CGRectMake(0, 0, frameCustom.size.width, MuneHeight);
                scrollView.contentSize = CGSizeMake(frameCustom.size.width * 2, MuneHeight);
            }else{
                scrollView.frame = CGRectMake(0, 0, frameCustom.size.width, MuneHeight);
            }
        }else{
            scrollView.frame = CGRectMake(0, 0, frameCustom.size.width, frameCustom.size.height / 2 + 25);
        }
        //
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        //
        scrollView.showsHorizontalScrollIndicator = NO;
        [scrollView addSubview:_backView1];
        [scrollView addSubview:_backView2];
        [self addSubview:scrollView];
        
        //
        for(int i = 0; i < titleArray.count; i++) {
            if(i < Item) {
                
                CGRect frame = CGRectMake(i*frameCustom.size.width/Item, 0, frameCustom.size.width/Item, 90);
                NSString *title = titleArray[i];
                NSString *imagestr = imageArray[i];
                TSMenuBtnView *btnView = [[TSMenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView1 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
                
            } else if (i < Item*2) {
                
                CGRect frame = CGRectMake((i-Item)*frameCustom.size.width/Item, 90, frameCustom.size.width/Item, 90);
                NSString *title = titleArray[i];
                NSString *imagestr = imageArray[i];
                TSMenuBtnView *btnView = [[TSMenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView1 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
                
            }else if (i < Item*3) {
                CGRect frame = CGRectMake((i-Item*2)*frameCustom.size.width/Item, 0, frameCustom.size.width/Item, 90);
                NSString *title = titleArray[i];
                NSString *imagestr = imageArray[i];                TSMenuBtnView *btnView = [[TSMenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView2 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
            } else {
                CGRect frame = CGRectMake((i-Item*3)*frameCustom.size.width/Item, 90, frameCustom.size.width/Item, 90);
                NSString *title = titleArray[i];
                NSString *imagestr = imageArray[i];                TSMenuBtnView *btnView = [[TSMenuBtnView alloc]initWithFrame:frame title:title imagestr:imagestr];
                btnView.tag = TAG + i;
                [_backView2 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Clicktap:)];
                [btnView addGestureRecognizer:tap];
            }
        }
//        if (titleArray.count < 8) {
//            return self;
//        }
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 80, MuneHeight-30, 160, 20)];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = titleArray.count > Item*2 ? 2 : 0;
        //        self.backgroundColor = [UIColor redColor];
        [self addSubview:_pageControl];
        [_pageControl setCurrentPageIndicatorTintColor:ThemeColor];
        [_pageControl setPageIndicatorTintColor:[HexColor(@"ea4a96") colorWithAlphaComponent:0.5]];
    }
    
    return self;
}


#pragma mark - UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollViewW/2)/scrollViewW;
    _pageControl.currentPage = page;
}

-(void)Clicktap:(UITapGestureRecognizer *)sender{
    NSLog(@"tag:%ld",sender.view.tag);
    [self.delegate muneViewButtonClick:sender.view.tag];
}



@end
